package cn.bat.slsystem.control;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bat.slsystem.pojo.AuNotice;
import cn.bat.slsystem.service.NoticeService;
import cn.bat.slsystem.tools.Constants;

@Controller
@RequestMapping(value="/notice")
public class NoticeController {
	@Autowired
	private NoticeService noticeService;
	
	@RequestMapping(value="/noticelist.html")
	public String list(HttpSession session){
		List<String> list = (List) session.getAttribute(Constants.lanjie);
		for (String path : list) {
			if(path.equals("notice/noticelist.html")){
				return "noticelist";
			}
		}
		return "login";
	}
	/*
	 * 显示公告信息
	 */
	@RequestMapping(value="/show.html")
	@ResponseBody
	public Object rolelist(Integer id){
		List<AuNotice> list = noticeService.selectByPrimaryKey(id);
		/*Map<String,Object> map =new HashMap<String,Object>();
		map.put("rolelist", list);*/
		return list;
	}
	/*
	 * 显示公告信息
	 */
	@RequestMapping(value="/show1.html")
	public Object rolelist1(Integer id,Map<String,Object> map){
		List<AuNotice> list = noticeService.selectByPrimaryKey(id);
		for (AuNotice auNotice : list) {
			map.put("noticeshow", auNotice);
		}
		return "noticeshow";
	}
	/*修改公告信息*/
	@RequestMapping(value="/update.html")
	public Object updaterole(AuNotice notice){
		notice.setIssuer("admin");
		System.out.println(notice.getStartingtime()+"+++++++++++++++++++++");
		int count = noticeService.updateByPrimaryKeySelective(notice);
		if(count>0){
			return "redirect:noticelist.html";
		}else{
			return "redirect:noticelist.html";
		}
	}
	
	/*根据id删除公告信息*/
	@RequestMapping(value="/delete.html")
	@ResponseBody
	public String del(Integer id){
		if(noticeService.deleteByPrimaryKey(id)==1){
			return "ok";
		}
		return "no";
	}
	
	/*添加公告信息*/
	@RequestMapping(value="/add.html")
	public String addterole(AuNotice notice,HttpSession session){
		System.out.println(notice.getStartingtime()+"+++++++++++++++");
		notice.setIssuer("admin");
		notice.setReleasetime(new Date());
			int count = noticeService.insertSelective(notice);
			if(count>0){
				session.setAttribute("show", "添加成功！");
				return "redirect:noticelist.html";
			}else{
				session.setAttribute("show", "添加失败！");
				return "redirect:noticelist.html";
			}
		
	}
}
